<?php

namespace app\admin\controller\questionlist;

use app\common\controller\Backend;
use app\admin\model\teacher\Teacher;
use think\Db;
use think\Exception;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Questionlist extends Backend
{
    
    /**
     * Questionlist模型对象
     * @var \app\admin\model\questionlist\Questionlist
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\questionlist\Questionlist;
        //分类
        $this->view->assign("ClassList", model('\app\admin\model\allclass\Allclass')->getClassList());
        //标签
        $this->view->assign("TagsList", model('\app\admin\model\allclass\Tags')->getTagsList());

        //教师列表
        $teacherList = collection((new Teacher())->where('status',0)->select())->toArray();
        $this->view->assign("teacherList", $teacherList);
    
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where(array('status' => array('NEQ', 9)))
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $k => $v) {
                //分类
                $list[$k]['classid'] = model('\app\admin\model\allclass\Allclass')->get_class_name($v['classid']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 选择问题
     */
    public function select()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where(array('status' => array('NEQ', 9)))
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $k => $v) {
                //分类
                $list[$k]['classid'] = model('\app\admin\model\allclass\Allclass')->get_class_name($v['classid']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {

                $params['createtime'] = time();

                $result = false;
                try {
                    $result = $this->model->allowField(true)->insertGetId($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {   
        $rows = $this->model->get($ids)->toArray();
        if (!$rows) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $this->token();
            $row = $this->model->get(['id' => $ids]);
            if (!$row) {
                $this->error(__('No Results were found'));
            }
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) { 

                $params['updatetime'] = time();

                Db::startTrans();
                try {
                    $row->save($params);
                    Db::commit();
                    $this->success();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
            }
            $this->error();
            return;
        }

        $this->view->assign("row", $rows);
        return $this->view->fetch();

    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $this->model->where('id', $ids)->update(['status' => '9']);
        $this->success();
    }

 


}
